<template>
  <div class="home_container">
    <el-container>
      <!-- 頭部區域 -->
      <el-header>
        <div>
          <img src="../assets/heima.png" alt="" />
          <span>電商後臺管理系統</span>
        </div>
        <el-button @click="logout()" type="info">退出</el-button>
      </el-header>

      <!-- 主體區域 -->
      <el-container>
        <!-- 側邊欄 -->
        <el-aside :width="isCollapsed ? '64px' : '200px'">
          <div class="toggle_button" @click="toggleCollapse()">|||</div>
          <!-- 一級菜單 -->
          <el-menu
            background-color="#333744"
            text-color="#fff"
            unique-opened
            :collapse="isCollapsed"
            :collapse-transition="false"
            :default-active="$router.path"
            router
          >
            <el-submenu
              v-for="(item, i) in menuList"
              :key="item.id"
              :index="'/' + item.path"
            >
              <template slot="title">
                <i :class="iconArr[i]"></i>
                <span>{{ item.authName }}</span>
              </template>

              <!-- 二級菜單 -->
              <el-menu-item
                v-for="twoItem in item.children"
                :key="twoItem.id"
                :index="'/' + twoItem.path"
              >
                <template slot="title">
                  <i class="el-icon-menu"></i>
                  <span v-cloak>{{ twoItem.authName }}</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-menu>
        </el-aside>

        <!-- 内容區域 -->
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  created() {
    this.getMenuList()
  },
  data() {
    return {
      menuList: [],
      iconArr: [
        'iconfont icon-users',
        'iconfont icon-tijikongjian',
        'iconfont icon-shangpin',
        'iconfont icon-danju',
        'iconfont icon-baobiao'
      ],
      isCollapsed: false
    }
  },
  methods: {
    logout() {
      // 1.清空 token
      window.sessionStorage.clear()

      // 2.重定向到登录页面
      this.$router.push('/login')
    },
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error('獲取列表失敗')
      this.$message.success('獲取列表成功')
      console.log(res)
      this.menuList = res.data
    },
    // 側邊欄菜單的摺叠與展開
    toggleCollapse() {
      this.isCollapsed = !this.isCollapsed
    }
  }
}
</script>

<style lang="less" scoped>
.el-header {
  display: flex;
  justify-content: space-between;
  padding: 0;
  color: #fff;
  font-size: 20;
  background-color: #373d41;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 15px;
    }
  }
  .el-button {
    margin: 10px;
  }
}

.el-aside {
  background-color: #333744;
  .el-menu {
    border-right: none;
  }
}

.el-main {
  background-color: #eaedf1;
}

.home_container {
  height: 100%;
  .el-container {
    height: 100%;
  }
}
.toggle_button {
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #4a5064;
  font-size: 10px;
  height: 30px;
  color: #fff;
  letter-spacing: 0.2em;
  cursor: pointer;
}
.iconfont {
  font-size: 20px;
  margin-right: 10px;
}
</style>
